
package XMLParser;

import java.util.ArrayList;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

// using SAX
public class SaxParserLib
{
    ArrayList<String> titleList = new ArrayList<String>();



    class HowToHandler extends DefaultHandler
    {
        boolean title = false;
        boolean url   = false;
        boolean finalI = false;


        @Override
        public void startElement(String nsURI,
                                 String strippedName,
                                 String tagName,
                                 Attributes attributes)  throws SAXException
        {

            if (tagName.equalsIgnoreCase("title"))
                title = true;
            if (tagName.equalsIgnoreCase("url"))
                url = true;
            if(tagName.equalsIgnoreCase("final"))
                finalI = true;


        }

        @Override
        public void characters(char[] ch, int start, int length)
        {

           if (title)
           {               
          
            System.out.println("Title: " + new String(ch,start,length) );
            String str = new String(ch, start, length);
            titleList.add(str);
            title = false;
       }
     else if (url) {
       System.out.println("Url: " + new String(ch, start,length));
       url = false;
       }
      else if (finalI) {
       System.out.println("Final: " + new String(ch, start,length));
       finalI = false;
       }


             }

    }
    public void list( ) throws Exception {
       XMLReader parser =
          XMLReaderFactory.createXMLReader();
            //("org.apache.crimson.parser.XMLReaderImpl");
       parser.setContentHandler(new HowToHandler( ));
       parser.parse("bla.xml");
       }


    public static void main(String[] args) throws Exception {
       SaxParserLib p = new SaxParserLib();
       p.list();
       for(int i = 0; i<p.titleList.size(); i++)
           System.out.println(p.titleList.get(i)+" ");
       }
}













/*


package XMLParser;

import XMLParser.XMLTags;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

// using SAX
public class SaxParserLib
{
    ArrayList<String> titleList = new ArrayList<String>();
    HashMap<XMLTags, Boolean> hash = new HashMap<XMLTags, Boolean>();

    

    

    class HowToHandler extends DefaultHandler
    {



        boolean title = false;
        boolean url   = false;
        boolean finalI = false;

        
        @Override
        public void startElement(String nsURI,
                                 String strippedName,
                                 String tagName,
                                 Attributes attributes)  throws SAXException
        {


            if(tagName != null)
            {
               // for(int i = 0; i < XMLTags.values().length;i++)
                XMLTags tag = XMLTags.angajati;
                if(hash.containsKey(tag))
                {
                    hash.remove(tag);
                    hash.put(tag, true);
                }


            }
     /*if (tagName.equalsIgnoreCase("topic"))
        title = true;
     if (tagName.equalsIgnoreCase("url"))
        url = true;
     if(tagName.equalsIgnoreCase("final"))
         finalI = true;


    }

        @Override
    public void characters(char[] ch, int start, int length) {

    /* if (title) {
       System.out.println("Title: " + 5);//new String(ch, start, length));
         //String str = new String(ch, start, length);
        // titleList.add(str);
       title = false;
       }
     else if (url) {
       System.out.println("Url: " + new String(ch, start,length));
       url = false;
       }
      else if (finalI) {
       System.out.println("Final: " + new String(ch, start,length));
       finalI = false;
       }*
       Boolean b = hash.get(XMLTags.angajati);
       if(b){
         System.out.println("Angajati: " + 5);//new String(ch, start, length));
         
         //String str = new String(ch, start, length);
        // titleList.add(str);
             hash.remove(XMLTags.angajati);
             hash.put(XMLTags.angajati, false);
        }
     }
    }

    public void list( ) throws Exception {
       XMLReader parser =
          XMLReaderFactory.createXMLReader();
            //("org.apache.crimson.parser.XMLReaderImpl");
       parser.setContentHandler(new HowToHandler( ));
       parser.parse("bla.xml");
       }

    public static void main(String[] args) throws Exception {
       SaxParserLib p = new SaxParserLib();
       p.list();
       for(int i = 0; i<p.titleList.size(); i++)
           System.out.println(p.titleList.get(i)+" ");
       }
}**/